package com.wysql.ascost.service;


import cn.hutool.core.util.StrUtil;
import com.wysql.ascost.common.base.BaseService;
import com.wysql.ascost.common.support.ConvertUtil;
import com.wysql.ascost.mapper.auto.UnitMapper;
import com.wysql.ascost.model.auto.Unit;
import com.wysql.ascost.model.auto.UnitExample;
import com.wysql.ascost.model.custom.Tablepar;
import com.wysql.ascost.util.SnowflakeIdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 单位表 UnitService
 *
 * @author fuce_自动生成
 * @Title: UnitService.java 
 * @Package com.wysql.ascost.service 
 * @email ${email}
 * @date 2021-05-28 10:59:21  
 **/
@Service
@RequiredArgsConstructor
public class UnitService implements BaseService<Unit, UnitExample> {
    private final UnitMapper unitMapper;


    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public PageInfo<Unit> list(Tablepar tablepar, Unit unit) {
        UnitExample testExample = new UnitExample();
        //搜索
        if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
            testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
        } else {//大搜索
            testExample.createCriteria().andLikeQuery(unit);
        }
        //表格排序
        //if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
        //}else{
        //	testExample.setOrderByClause("id ASC");
        //}
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<Unit> list = unitMapper.selectByExample(testExample);
        PageInfo<Unit> pageInfo = new PageInfo<Unit>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {

        List<String> lista = ConvertUtil.toListStrArray(ids);
        UnitExample example = new UnitExample();
        example.createCriteria().andIdIn(lista);
        return unitMapper.deleteByExample(example);


    }


    @Override
    public Unit selectByPrimaryKey(String id) {

        return unitMapper.selectByPrimaryKey(id);

    }


    @Override
    public int updateByPrimaryKeySelective(Unit record) {
        return unitMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(Unit record) {

        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());


        return unitMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(Unit record, UnitExample example) {

        return unitMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(Unit record, UnitExample example) {

        return unitMapper.updateByExample(record, example);
    }

    @Override
    public List<Unit> selectByExample(UnitExample example) {

        return unitMapper.selectByExample(example);
    }


    @Override
    public long countByExample(UnitExample example) {

        return unitMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(UnitExample example) {

        return unitMapper.deleteByExample(example);
    }


    public List<Unit> queryUnit(Unit unit) {
        return unitMapper.queryUnit(unit);
    }
}
